package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IRequestingForCoordinatorDAO;
import com.AAL.Model.Employee;
import com.AAL.Model.RequestingForCoordinator;
import com.AAL.Utils.HibernateUtil;

public class RequestingForCoordinatorDAO implements
		IRequestingForCoordinatorDAO {

	@Override
	public RequestingForCoordinator saveAndUpdateRequestingForCoordinator(
			RequestingForCoordinator requestingForCoordinator) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(requestingForCoordinator);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return requestingForCoordinator;
	}

	@Override
	public RequestingForCoordinator deleteRequestingForCoordinator(
			int requestingForCoordinatorID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			RequestingForCoordinator requestingForCoordinator = (RequestingForCoordinator) session
					.load(RequestingForCoordinator.class,
							requestingForCoordinatorID);
			if (null != requestingForCoordinator) {
				session.delete(requestingForCoordinator);
			}
			session.getTransaction().commit();
			return requestingForCoordinator;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public RequestingForCoordinator getRequestingForCoordinator(
			int requestingForCoordinatorID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		RequestingForCoordinator requestingForCoordinator = (RequestingForCoordinator) session
				.load(RequestingForCoordinator.class,
						requestingForCoordinatorID);
		return requestingForCoordinator;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RequestingForCoordinator> getLstRequestingForCoordinatorsByEmployee(
			Employee employee) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<RequestingForCoordinator> lstRequestingForCoordinators = null;
		try {
			lstRequestingForCoordinators = (List<RequestingForCoordinator>) session
					.createQuery(
							"from RequestingForCoordinator where employeeByHO.employeeID=:employeeID")
					.setParameter("employeeID", employee.getEmployeeID())
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstRequestingForCoordinators;
	}

}
